package com.xy.biz.mc.service.impl;

import com.xy.biz.mc.domain.bo.UserConnectionBO;
import com.xy.biz.mc.domain.query.UserConnectionQuery;
import com.xy.biz.mc.manager.UserConnectionManager;
import com.xy.biz.mc.service.UserConnectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class UserConnectionServiceImpl implements UserConnectionService {

    @Autowired
    private UserConnectionManager userConnectionManager;

    @Override
    public void register(UserConnectionBO userConnectionBO) {
        LocalDateTime now = LocalDateTime.now();
        userConnectionBO.setConnectTime(LocalDateTime.now());
        userConnectionBO.setStatus(UserConnectionBO.STATUS_CONNECT);
        userConnectionBO.setCreateTime(now);
        userConnectionBO.setUpdateTime(now);
        userConnectionManager.save(userConnectionBO);
    }

    @Override
    public void unregister(UserConnectionBO userConnectionBO) {
        userConnectionBO.setDisconnectTime(LocalDateTime.now());
        userConnectionBO.setStatus(UserConnectionBO.STATUS_DISCONNECT);
        userConnectionBO.setUpdateTime(LocalDateTime.now());
        userConnectionManager.update(userConnectionBO);
    }

    @Override
    public Long getServerId(Long userId) {
        return userConnectionManager.getServerId(userId);
    }

    @Override
    public Map<Long, Long> listServerId(Collection<Long> userIdCollection) {
        return userConnectionManager.listServerId(userIdCollection);
    }

    @Override
    public Map<Long, Long> listServerIdByUserGroupId(Long userGroupId) {
        return userConnectionManager.listServerIdByUserGroupId(userGroupId);
    }

    @Override
    public List<UserConnectionBO> listUserConnection(UserConnectionQuery query) {
        return userConnectionManager.listUserConnection(query);
    }

    @Override
    public int countUserConnection(UserConnectionQuery query) {
        return userConnectionManager.countUserConnection(query);
    }

}
